//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2025-11-02  14-50-41 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2025-11-02  14-50-41 
//===============================





using System.Collections.Generic;
using System;

public class GongfaGenerator
{
    // 功法名称部件
    private static readonly string[] namePrefixes = {
        "太极", "无极", "九阳", "九阴", "乾坤", "阴阳", "天地", "五行",
        "八荒", "六合", "七星", "两仪", "混元", "先天", "后天", "混沌"
    };

    private static readonly string[] nameSuffixes = {
        "神功", "大法", "真经", "宝典", "诀", "咒", "印", "掌",
        "拳", "剑法", "刀法", "枪法", "指", "腿", "身法", "心法"
    };

    private static readonly string[] elementSuffixes = {
        "", "炎", "冰", "雷", "风", "土", "木", "金", "光", "暗"
    };

    // 描述模板
    private static readonly Dictionary<GongfaType, string[]> descriptionTemplates = new Dictionary<GongfaType, string[]>
    {
        {
            GongfaType.Attack, new string[] {
                "以{0}之力凝聚于掌心，发出致命一击",
                "运转{0}之息，化作凌厉攻势",
                "{0}元素在体内奔腾，爆发惊人威力"
            }
        },
        {
            GongfaType.Defense, new string[] {
                "{0}元素形成坚固护盾，抵御外敌",
                "体内{0}之力流转，强化肉身防御",
                "借天地{0}之势，筑起无形屏障"
            }
        },
        {
            GongfaType.Support, new string[] {
                "{0}之息滋养经脉，恢复元气",
                "引导{0}元素，为同伴施加增益",
                "{0}之力在体内循环，提升修为"
            }
        },
        {
            GongfaType.Movement, new string[] {
                "身如{0}影，飘忽不定",
                "脚踏{0}云，瞬息千里",
                "{0}之力灌注双腿，身法灵动"
            }
        },
        {
            GongfaType.Cultivation, new string[] {
                "吸纳天地{0}精华，淬炼金丹",
                "{0}元素在丹田凝聚，突破瓶颈",
                "感悟{0}之道，提升修为境界"
            }
        }
    };

    // 生成随机功法
    public static Gongfa GenerateGongfa(int playerLevel = 1, GongfaRarity forcedRarity = GongfaRarity.Common)
    {
        Gongfa gongfa = new Gongfa();

        // 确定品质
        gongfa.rarity = forcedRarity != GongfaRarity.Common ? forcedRarity : GetRandomRarity();

        // 生成基础属性
        GenerateBasicAttributes(gongfa, playerLevel);

        // 生成名称和描述
        GenerateNameAndDescription(gongfa);

        // 生成特性
        GenerateTraits(gongfa);

        return gongfa;
    }

    // 生成指定类型的功法
    public static Gongfa GenerateSpecificGongfa(GongfaType type, GongfaElement element, int playerLevel = 1)
    {
        Gongfa gongfa = GenerateGongfa(playerLevel);
        gongfa.type = type;
        gongfa.element = element;

        // 重新生成名称和描述以匹配指定类型和属性
        GenerateNameAndDescription(gongfa);

        return gongfa;
    }

    private static GongfaRarity GetRandomRarity()
    {
        float rand = UnityEngine.Random.value;

        if (rand < 0.5f) return GongfaRarity.Common;      // 50%
        else if (rand < 0.75f) return GongfaRarity.Uncommon; // 25%
        else if (rand < 0.9f) return GongfaRarity.Rare;      // 15%
        else if (rand < 0.98f) return GongfaRarity.Epic;     // 8%
        else return GongfaRarity.Legendary;                 // 2%
    }

    private static void GenerateBasicAttributes(Gongfa gongfa, int playerLevel)
    {
        // 随机类型和属性
        gongfa.type = (GongfaType)UnityEngine.Random.Range(0, System.Enum.GetValues(typeof(GongfaType)).Length);
        gongfa.element = (GongfaElement)UnityEngine.Random.Range(0, System.Enum.GetValues(typeof(GongfaElement)).Length);

        // 基于品质和玩家等级生成属性
        float rarityMultiplier = 1f + (int)gongfa.rarity * 0.3f;
        float levelMultiplier = 1f + playerLevel * 0.1f;

        gongfa.level = Math.Max(1, playerLevel + UnityEngine.Random.Range(-2, 3));
        gongfa.power = UnityEngine.Random.Range(10f, 50f) * rarityMultiplier * levelMultiplier;
        gongfa.cost = UnityEngine.Random.Range(5f, 20f) / rarityMultiplier;
        gongfa.cooldown = UnityEngine.Random.Range(1f, 10f) / rarityMultiplier;

        // 根据类型调整属性
        AdjustAttributesByType(gongfa);
    }

    private static void AdjustAttributesByType(Gongfa gongfa)
    {
        switch (gongfa.type)
        {
            case GongfaType.Attack:
                gongfa.power *= 1.3f;
                gongfa.cost *= 1.2f;
                break;
            case GongfaType.Defense:
                gongfa.power *= 0.9f;
                gongfa.cost *= 0.8f;
                break;
            case GongfaType.Support:
                gongfa.power *= 0.8f;
                gongfa.cooldown *= 1.5f;
                break;
            case GongfaType.Movement:
                gongfa.cost *= 0.7f;
                gongfa.cooldown *= 0.8f;
                break;
            case GongfaType.Cultivation:
                gongfa.power *= 1.1f;
                gongfa.cost *= 1.5f;
                break;
        }
    }

    private static void GenerateNameAndDescription(Gongfa gongfa)
    {
        string prefix = namePrefixes[UnityEngine.Random.Range(0, namePrefixes.Length)];
        string suffix = nameSuffixes[UnityEngine.Random.Range(0, nameSuffixes.Length)];
        string elementSuffix = elementSuffixes[(int)gongfa.element];

        gongfa.name = $"{prefix}{elementSuffix}{suffix}";

        // 生成描述
        string[] templates = descriptionTemplates[gongfa.type];
        string template = templates[UnityEngine.Random.Range(0, templates.Length)];
        string elementName = GetElementName(gongfa.element);

        gongfa.description = string.Format(template, elementName);

        // 添加品质描述
        string rarityText = GetRarityText(gongfa.rarity);
        gongfa.description += $"。{rarityText}";

        // 添加特性描述
        if (gongfa.traits != GongfaTraits.None)
        {
            string traitsDesc = GetTraitsDescription(gongfa.traits);
            gongfa.description += traitsDesc;
        }
    }

    private static void GenerateTraits(Gongfa gongfa)
    {
        GongfaTraits availableTraits = GetAvailableTraits(gongfa.type);
        int maxTraits = Math.Min((int)gongfa.rarity + 1, 3);
        int traitCount = UnityEngine.Random.Range(0, maxTraits + 1);

        for (int i = 0; i < traitCount; i++)
        {
            GongfaTraits newTrait = GetRandomTrait(availableTraits);
            if (newTrait != GongfaTraits.None)
            {
                gongfa.traits |= newTrait;
                // 移除已选择的特性，避免重复
                availableTraits &= ~newTrait;
            }
        }
    }

    private static GongfaTraits GetAvailableTraits(GongfaType type)
    {
        switch (type)
        {
            case GongfaType.Attack:
                return GongfaTraits.AreaEffect | GongfaTraits.LifeSteal | GongfaTraits.Stun |
                       GongfaTraits.Poison | GongfaTraits.Burn | GongfaTraits.Critical |
                       GongfaTraits.MultiHit | GongfaTraits.Channeling;
            case GongfaType.Defense:
                return GongfaTraits.Shield | GongfaTraits.Stun | GongfaTraits.Freeze |
                       GongfaTraits.Debuff | GongfaTraits.Channeling;
            case GongfaType.Support:
                return GongfaTraits.AreaEffect | GongfaTraits.Heal | GongfaTraits.Buff |
                       GongfaTraits.Debuff | GongfaTraits.Channeling;
            case GongfaType.Movement:
                return GongfaTraits.AreaEffect | GongfaTraits.Stun | GongfaTraits.Buff;
            case GongfaType.Cultivation:
                return GongfaTraits.Buff | GongfaTraits.Heal | GongfaTraits.Channeling;
            default:
                return GongfaTraits.None;
        }
    }

    private static GongfaTraits GetRandomTrait(GongfaTraits availableTraits)
    {
        List<GongfaTraits> available = new List<GongfaTraits>();
        foreach (GongfaTraits trait in System.Enum.GetValues(typeof(GongfaTraits)))
        {
            if (trait != GongfaTraits.None && availableTraits.HasFlag(trait))
                available.Add(trait);
        }

        if (available.Count == 0) return GongfaTraits.None;

        return available[UnityEngine.Random.Range(0, available.Count)];
    }

    private static string GetElementName(GongfaElement element)
    {
        switch (element)
        {
            case GongfaElement.Fire: return "火焰";
            case GongfaElement.Water: return "水流";
            case GongfaElement.Earth: return "大地";
            case GongfaElement.Wood: return "草木";
            case GongfaElement.Metal: return "金属";
            case GongfaElement.Lightning: return "雷霆";
            case GongfaElement.Wind: return "疾风";
            case GongfaElement.Ice: return "寒冰";
            case GongfaElement.Dark: return "黑暗";
            case GongfaElement.Light: return "光明";
            default: return "无形";
        }
    }

    private static string GetRarityText(GongfaRarity rarity)
    {
        switch (rarity)
        {
            case GongfaRarity.Common: return "基础功法";
            case GongfaRarity.Uncommon: return "精妙功法";
            case GongfaRarity.Rare: return "玄妙功法";
            case GongfaRarity.Epic: return "神奇功法";
            case GongfaRarity.Legendary: return "无上功法";
            default: return "普通功法";
        }
    }

    private static string GetTraitsDescription(GongfaTraits traits)
    {
        List<string> traitDescs = new List<string>();

        if (traits.HasFlag(GongfaTraits.AreaEffect)) traitDescs.Add("范围效果");
        if (traits.HasFlag(GongfaTraits.LifeSteal)) traitDescs.Add("生命汲取");
        if (traits.HasFlag(GongfaTraits.Stun)) traitDescs.Add("眩晕控制");
        if (traits.HasFlag(GongfaTraits.Poison)) traitDescs.Add("剧毒侵蚀");
        if (traits.HasFlag(GongfaTraits.Burn)) traitDescs.Add("灼烧伤害");
        if (traits.HasFlag(GongfaTraits.Freeze)) traitDescs.Add("冰冻效果");
        if (traits.HasFlag(GongfaTraits.Shield)) traitDescs.Add("护盾加持");
        if (traits.HasFlag(GongfaTraits.Heal)) traitDescs.Add("治疗恢复");
        if (traits.HasFlag(GongfaTraits.Buff)) traitDescs.Add("属性增益");
        if (traits.HasFlag(GongfaTraits.Debuff)) traitDescs.Add("负面效果");
        if (traits.HasFlag(GongfaTraits.Critical)) traitDescs.Add("暴击加成");
        if (traits.HasFlag(GongfaTraits.MultiHit)) traitDescs.Add("多重打击");
        if (traits.HasFlag(GongfaTraits.Channeling)) traitDescs.Add("持续施法");

        if (traitDescs.Count > 0)
            return "。蕴含特性：" + string.Join("、", traitDescs);

        return "";
    }
}
